package cn.iocoder.yudao.module.scrm.controller.admin.marketing;

import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.visitor.MarketingVisitorPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.visitor.MarketingVisitorRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.marketing.vo.visitor.MarketingVisitorSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.marketing.MarketingVisitorDO;
import cn.iocoder.yudao.module.scrm.service.marketing.visitor.MarketingVisitorService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 营销访客数据")
@RestController
@RequestMapping("/scrm/marketing-visitor")
@Validated
public class MarketingVisitorController {

    @Resource
    private MarketingVisitorService marketingVisitorService;

    @PostMapping("/create")
    @Operation(summary = "创建营销访客数据")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-visitor:create')")
    public CommonResult<Integer> createMarketingVisitor(@Valid @RequestBody MarketingVisitorSaveReqVO createReqVO) {
        return success(marketingVisitorService.createMarketingVisitor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新营销访客数据")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-visitor:update')")
    public CommonResult<Boolean> updateMarketingVisitor(@Valid @RequestBody MarketingVisitorSaveReqVO updateReqVO) {
        marketingVisitorService.updateMarketingVisitor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除营销访客数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:marketing-visitor:delete')")
    public CommonResult<Boolean> deleteMarketingVisitor(@RequestParam("id") Integer id) {
        marketingVisitorService.deleteMarketingVisitor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得营销访客数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-visitor:query')")
    public CommonResult<MarketingVisitorRespVO> getMarketingVisitor(@RequestParam("id") Integer id) {
        MarketingVisitorDO marketingVisitor = marketingVisitorService.getMarketingVisitor(id);
        return success(BeanUtils.toBean(marketingVisitor, MarketingVisitorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得营销访客数据分页")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-visitor:query')")
    public CommonResult<PageResult<MarketingVisitorRespVO>> getMarketingVisitorPage(@Valid MarketingVisitorPageReqVO pageReqVO) {
        PageResult<MarketingVisitorDO> pageResult = marketingVisitorService.getMarketingVisitorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MarketingVisitorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出营销访客数据 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:marketing-visitor:export')")
    @OperateLog(type = EXPORT)
    public void exportMarketingVisitorExcel(@Valid MarketingVisitorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MarketingVisitorDO> list = marketingVisitorService.getMarketingVisitorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "营销访客数据.xls", "数据", MarketingVisitorRespVO.class,
                        BeanUtils.toBean(list, MarketingVisitorRespVO.class));
    }

}